//
//  YuanXinTrack.m
//  YuanXinTrack
//
//  Created by GJS on 2018/5/9.
//  Copyright © 2018年 GJS. All rights reserved.
//

#import "YuanXinTrack.h"
#import "YuanXinTrackDataManager.h"

@implementation YuanXinTrack

RCT_EXPORT_MODULE();

#pragma mark -- export method to js
/**
 设置一些统一选项

 @param NSDictionary options
 */
RCT_EXPORT_METHOD(setOptions:(NSDictionary *)options) {
    [YuanXinTrackDataManager sharedManager].numbersBeginUpload = [options[@"numbersBeginUpload"] integerValue];
}

/**
 用户行为日志记录

 @param NSDictionary options
 */
RCT_EXPORT_METHOD(recordTrackLog:(NSDictionary *)options  reslover:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)recject){
    
    YuanXinTrackModel *model = [YuanXinTrackModel new];
    NSString *ID = options[@"id"];
    if (ID && ID.length > 0) {
        model.ID = ID;
    } else {
        model.ID = model.uuid;
    }
    model.userID = [YuanXinTrackDataManager sharedManager].currentUserID;
    model.logLot = options[@"logLot"] ?: @"";
    model.eventType = options[@"eventType"] ?: @"";
    model.eventName = options[@"eventName"] ?: @"";
    // 如果 time 是字符串，用model默认的格式转换
    id eventTime = options[@"eventTime"];
    model.eventTime = [eventTime isKindOfClass:[NSString class]] ? [model dateFromStr:eventTime formatStr:nil] : [RCTConvert NSDate:eventTime];
    NSMutableDictionary *mDict = [options[@"eventData"] mutableCopy];
    NSString *startTime = [model strFromDate:[RCTConvert NSDate:mDict[@"startTime"]] formatStr:nil];
    NSString *endTime = [model strFromDate:[RCTConvert NSDate:mDict[@"endTime"]] formatStr:nil];
    if (startTime) {
        [mDict setObject:startTime forKey:@"startTime"];
    }
    if (endTime) {
        [mDict setObject:endTime forKey:@"endTime"];
    }
    model.eventData = mDict;
    
    BOOL result = [[YuanXinTrackDataManager sharedManager] recordTrackLog:model];
    
    if (resolve) {
        resolve(@(result));
    }
}

/**
 用户登录日志记录

 @param NSDictionary options
 */
RCT_EXPORT_METHOD(recordLoginLog:(NSDictionary *)options  reslover:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)recject){
    
    [YuanXinTrackDataManager sharedManager].currentUserID = options[@"userID"];
    
    YuanXinTrackModel *model = [YuanXinTrackModel new];
    NSString *ID = options[@"id"];
    if (ID && ID.length > 0) {
        model.ID = ID;
    } else {
        model.ID = model.uuid;
    }
    model.userID = [YuanXinTrackDataManager sharedManager].currentUserID;
    model.logLot = options[@"logLot"] ?: @"";
    model.loginType = options[@"loginType"] ?: @"";
    // 如果 time 是字符串，用model默认的格式转换
    id loginTime = options[@"loginTime"];
    model.loginTime = [loginTime isKindOfClass:[NSString class]] ? [model dateFromStr:loginTime formatStr:nil] : [RCTConvert NSDate:loginTime];
    
    BOOL result = [[YuanXinTrackDataManager sharedManager] recordLoginLog:model];
    
    if (resolve) {
        resolve(@(result));
    }
}

/**
 用户其他日志记录
 
 @param NSDictionary options
 */
RCT_EXPORT_METHOD(recordAnyTrackLog:(NSDictionary *)options  reslover:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)recject){
    
    YuanXinTrackModel *model = [YuanXinTrackModel new];
    NSString *ID = options[@"id"];
    if (ID && ID.length > 0) {
        model.ID = ID;
    } else {
        model.ID = model.uuid;
    }
    model.userID = [YuanXinTrackDataManager sharedManager].currentUserID;
    model.eventData = options; // 原样赋值
    
    BOOL result = [[YuanXinTrackDataManager sharedManager] recordAnyTrackLog:model];
    
    if (resolve) {
        resolve(@(result));
    }
}

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getGuid)
{
    return NSUUID.UUID.UUIDString;
}

@end
